package com.itheima.reggie.common;



import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/*
//表明这个类为异常处理的类
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class ProjectExceptionAdvice {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> doEception(SQLIntegrityConstraintViolationException ex) {
        String message = ex.getMessage();
        log.info(message);
        if (message.contains("Duplicate entry")) {
            String[] split = message.split(" ");
            System.out.println(split[2]);
            String msg = split[2] + "已存在";
            log.info(msg);
            return R.error(msg);
        }
        return R.error("未知错误");

    }
}
*/
@ControllerAdvice(annotations = {Controller.class,RestController.class})
@ResponseBody
@Slf4j
public class ProjectExceptionAdvice {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> doEception(SQLIntegrityConstraintViolationException ex) {
        log.info(ex.getMessage());
        if (ex.getMessage().contains("Duplicate entry")) {
            String[] s = ex.getMessage().split(" ");
            String msg = s[2].replace("'","") + "已存在";
            return R.error(msg);
        }
        return R.error("未知错误");
    }

    @ExceptionHandler(CustomException.class)
    public R<String> doEception(CustomException ex) {

        return R.error(ex.getMessage());
    }
}